﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;
namespace DAO
{
    public class BOPHANDAO
    {

        public int update(DataTable datatable, string sqlquery)
        {
            int effrow = 0;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sqlquery;
            SqlConnection conn = DATAPROVIDER.ConnectSQL();
            cmd.Connection = conn;
            SqlDataAdapter sqldataadapter = new SqlDataAdapter(cmd);
            SqlCommandBuilder sqlcomandbuilder = new SqlCommandBuilder(sqldataadapter);
            effrow = sqldataadapter.Update(datatable);
            sqldataadapter.Dispose();

            conn.Close();
            conn.Dispose();

            return effrow;
        }

        public int thembophan(BOPHANDTO bpDTO)
        {
            SqlConnection conn = DATAPROVIDER.ConnectSQL();
            string sql = "INSERT INTO BO_PHAN VALUES(@mbp,@tbp)";
            SqlCommand sqlcommand = new SqlCommand(sql,conn);

            SqlParameter sqlparameter = new SqlParameter("@mbp",System.Data.SqlDbType.NChar,3);
            sqlparameter.Value = bpDTO.Mabophan;
            sqlcommand.Parameters.Add(sqlparameter);

            sqlparameter = new SqlParameter("@tbp", System.Data.SqlDbType.NVarChar, 50);
            sqlparameter.Value = bpDTO.Tenbophan;
            sqlcommand.Parameters.Add(sqlparameter);

            int kq = sqlcommand.ExecuteNonQuery();
            conn.Close();
            conn.Dispose();
            if (kq > 0)
            {
                BIENNHANVIENDAO bnnDAO = new BIENNHANVIENDAO();
                bnnDAO.thembien(bpDTO);
            }
            return kq;
        }

        public DataTable Loadbophan()
        {
            DataTable datatble = new DataTable();
            SqlConnection conn = DATAPROVIDER.ConnectSQL();
            string sql = "SELECT * FROM BO_PHAN";
            SqlDataAdapter sqldataadapter = new SqlDataAdapter(sql, conn);
            sqldataadapter.Fill(datatble);
            conn.Close();
            conn.Dispose();
            sqldataadapter.Dispose();
            return datatble;
        }
    }
}
